import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sun.org.apache.bcel.internal.generic.NEW;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

/**生成二维码
 * Created by ${chengwengao} on 2017/1/22.
 * 注：zxing创建和读取二维码的jar在同一个文件中，路径在zxing文件夹下，记得导入
 */
public class CreateQRcode {
    public static void main(String[] args){
        int width = 300;
        int height = 300;
        String format = "png";
        String content = "TiDB is a distributed NewSQL database compatible with MySQL protocol";

        //定义二维码参数
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN,2);

        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            Path path = new File("D:/QR.PNG").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix,format,path);
            System.out.println("二维码生成成功!");
            } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
